<?php

namespace App\Models;

use App\Models\Traits\BelongsToApplication;
use App\Models\Traits\HasEasyStatus;
use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
use EasyWeChat\Pay\Application;
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;

class Payment extends Model
{
    use BelongsToApplication,
        Cachable,
        HasEasyStatus,
        SoftDeletes;

    protected static function boot(): void
    {
        parent::boot();
    }

    public function orders(): HasMany
    {
        return $this->hasMany(Order::class);
    }

    /**
     * 初始化微信支付
     *
     * @throws InvalidArgumentException
     */
    public function init(): Application
    {
        $config = [
            'mch_id' => $this->mch_id,
            'private_key' => $this->key_file,
            'certificate' => $this->cert_file,
            'secret_key' => $this->key_v3,
            'v2_secret_key' => $this->key_v2,
            'platform_certs' => [
//                '53A1160530EE26EC3EEC6B52A97772CB9C5EE7BE' => storage_path('app/wechatpay_53A1160530EE26EC3EEC6B52A97772CB9C5EE7BE.pem'),
            ],
        ];

        return new Application($config);
    }
}
